//
//  StatusOriginalView.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/18.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//

import UIKit
import SnapKit

class StatusOriginalView: UIView {
    //绑定数据
    var statusModel: Status? {
        didSet {
            headImageView.sd_setImageWithURL(statusModel?.user?.headImageUrl, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = statusModel?.user?.name
            mbrankImageView.image = statusModel?.user?.mbrank_image
            verifiedTypeImgView.image = statusModel?.user?.verified_type_image
            created_atLabel.text = statusModel?.created_at
            sourceLabel.text = statusModel?.source
            textLabel.text = statusModel?.text
            bottomConstraint?.uninstall()
            
            statusPictureView.imageUrls = statusModel?.picImageUrls

            if ((statusPictureView.imageUrls?.count) == nil) {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(kStatusCellMargin).constraint
                })
                return
            }
            
            self.snp_updateConstraints { (make) -> Void in
                self.bottomConstraint = make.bottom.equalTo(statusPictureView.snp_bottom).offset(kStatusCellMargin).constraint
            }
        }
    }
    
    //用属性来保存约束,以便修改
    var bottomConstraint: Constraint?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor.redColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    func setupUI() {
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(verifiedTypeImgView)
        addSubview(created_atLabel)
        addSubview(sourceLabel)
        addSubview(textLabel)
        addSubview(statusPictureView)
        
        addConstraints()
    }
    
    //添加约束
    private func addConstraints() {
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(kStatusCellMargin)
            make.size.equalTo(kStatusCellSize)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(kStatusCellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(kStatusCellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        
        verifiedTypeImgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_right)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        created_atLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(kStatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.left.equalTo(created_atLabel.snp_right).offset(10)
        }
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImageView.snp_left)
            make.top.equalTo(headImageView.snp_bottom).offset(kStatusCellMargin)
//            make.right.equalTo(self.).offset(-kStatusCellMargin)
//            make.width.equalTo(screenSize.width - CGFloat(2 * kStatusCellMargin))
        }
        
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(kStatusCellMargin)
            make.left.equalTo(textLabel.snp_left)
//            make.size.equalTo(CGSize(width: 200, height: 100))
        }
        //MARK:/*一定不要忘了这一步,通过设置底部的约束可以自动计算行高*/
        self.snp_makeConstraints { (make) -> Void in
             bottomConstraint = make.bottom.equalTo(statusPictureView.snp_bottom).offset(kStatusCellMargin).constraint
        }
    }
    
    //MARK: 懒加载子控件
    lazy var headImageView: UIImageView  = UIImageView()
    lazy var nameLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 15)
    lazy var mbrankImageView: UIImageView = UIImageView()
    lazy var verifiedTypeImgView: UIImageView = UIImageView()
    lazy var created_atLabel: UILabel = UILabel(title: nil, color: UIColor.lightGrayColor(), fontSize: 12)
    lazy var sourceLabel: UILabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 12)
    lazy var textLabel: UILabel = UILabel(title: "", color: UIColor.blackColor(), fontSize: 15, margin: kStatusCellMargin)
    lazy var statusPictureView: StatusPictureView = StatusPictureView()
}
